package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.SendProductChangeTimBean;
import com.ymatou.iapi.livebuyer.service.SendProductChangeTimCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*接收商品变更通知发送云通讯101消息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendProductChangeTim{
	private static SendProductChangeTimBean sendproductchangetimBean;
	private static SendProductChangeTimCall sendproductchangetimCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("接收商品变更通知发送云通讯101消息");
	}

	@Before
	public void caseUp() {
		sendproductchangetimBean=new SendProductChangeTimBean();
		sendproductchangetimCall=new SendProductChangeTimCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendproductchangetimCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendProductChangeTim_001() {
		Logger.start(true,"接收商品变更通知发送云通讯101消息成功");
		try {
			LiveRoomWapper LiveRoomWapper=new LiveRoomWapper();
			int uid=2;
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 4*60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			sendproductchangetimBean.setRequestId(reqid);
			sendproductchangetimBean.setProductId(productId);
			sendproductchangetimCall.setData(sendproductchangetimBean);
			sendproductchangetimCall.callService();			
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(sendproductchangetimBean.getRequestId())).filter(s->s.contains("request body")).filter(s->s.contains("101")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true, filter.size()>0, "验证有发送101消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SendProductChangeTim_002() {
		Logger.start(true,"商品所在直播已经结束,不发送");
		try {
			LiveRoomWapper LiveRoomWapper=new LiveRoomWapper();
			int uid=2;
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 4*60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			LiveRoomCallService.AutoCloseLiveRoomByUserId(sellerId);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			sendproductchangetimBean.setRequestId(reqid);
			sendproductchangetimBean.setProductId(productId);
			sendproductchangetimCall.setData(sendproductchangetimBean);
			sendproductchangetimCall.callService();			
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(sendproductchangetimBean.getRequestId())).filter(s->s.contains("request body")).filter(s->s.contains("101")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true, filter.size()==0, "验证有发送101消息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendProductChangeTim_003() {
		Logger.start(true,"接收商品变更通知发送云通讯101消息成功、计数器+1");
		try {
			LiveRoomWapper LiveRoomWapper=new LiveRoomWapper();
			int uid=2;
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 4*60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			sendproductchangetimBean.setRequestId(reqid);
			sendproductchangetimBean.setProductId(productId);
			sendproductchangetimCall.setData(sendproductchangetimBean);

			Integer counter = CounterCallService.getSingleValueByLiveId(createlive.getId());
			sendproductchangetimCall.callService();	
			Integer counter1 = CounterCallService.getSingleValueByLiveId(createlive.getId());
			Logger.verifyEquals(counter+1, counter1, "添加商品之后，计数器+1");	
			
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains(sendproductchangetimBean.getRequestId())).filter(s->s.contains("request body")).filter(s->s.contains("101")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true, filter.size()>0, "验证有发送101消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}